function Track()
{
	this.trackpoints = new Array();
	this.numberOfPoints;
	this.gpxFileName;
}

Track.prototype.addTrackpoint = function(trackpoint)
{
	this.trackpoints.push(trackpoint);
}

Track.prototype.getTrackpoints = function()
{
	return this.trackpoints;
}

Track.prototype.setNumberOfPoints = function(value)
{
	this.numberOfPoints = value;
}

Track.prototype.raiseNumberOfPoints = function()
{
	this.numberOfPoints = this.numberOfPoints + 1;
}

Track.prototype.getNumberOfPoints = function()
{
	return this.numberOfPoints;
}

Track.prototype.initExport = function()
{
	//Javascript Popup um filename zu holen?
	console.log("Init Export");
	window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, this.gotFS, this.fail);
}

Track.prototype.gotFS = function(fileSystem) 
{
	console.log("gotFS Function");
   fileSystem.root.getFile("trackTest.txt", {"create":true}, this.gotFileEntry, this.fail);
}

Track.prototype.gotFileEntry = function(fileEntry) 
{
	console.log("GotFileEntry Function");
   fileEntry.createWriter(this.exportAsGPX, this.fail);
}

Track.prototype.fail = function(error)
{
	console.log("fail");
	  console.log(error.code);
}

Track.prototype.exportAsGPX = function(writer)
{
	console.log("exportAsGPX-Function");
	var canWrite = true;
	var eof = false;
	writer.onwrite = function(evt) {
		canWrite = true;
        console.log("write success");
    };
    canWrite = false;
    writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" creator=\"byHand\" version=\"1.1\"\nxmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">\n<rte>\n");
    var index = 0;
    while(eof == false)
    {
    	if(canWrite == true)
    	{
    		//Write One Trackpoint to GPX (Lat, Long, Heigth, Name)
    		canWrite = false;
    		writer.write("<rtept lat=\""+this.trackpoints[index].getLatitude()+"\" lon=\""+this.trackpoints[index].getLongitude()+"\">\n<ele>"+this.trackpoints[index].getHeigth()+"</ele>\n<time>"+this.trackpoints[index].getTimestamp()+"</time>\n<name>Point "+index+"</name>\n</rtept>/n")	;
    		index = index+1;
    		if(index == this.trackpoints.length)
    		{
    			eof = true;
    		}
    	}
    	else
    	{
    		//Do nothing
    	}
    		
    }
    
    while(true)
    {
    	if(canWrite == true)
    	{
    		//Write End of File
    		writer.write("</rte>/n</gpx>\n");
    		break;
    	}
    	else
    	{
    		//Do nothing
    	}
    }
}
